﻿@page "/Admin/Tenants"
@attribute [Authorize]
@attribute [TabItemOption(Text ="租户管理",Icon ="fa fa-th")]
@inherits BasePage
@inject DialogService DialogService
@inject ISysTenantService SysTenantService
@inject ISysMenuService SysMenuService

<div class="overflow-hidden h-100">
    <Table TItem="TenantOutput"
            IsPagination="true"
            IsStriped="true"
            IsMultipleSelect="true"
            IsBordered="false"
            IsFixedHeader="true" 
            AllowResizing="true"
            ShowLoading="true"
            ShowColumnList="true"
            ShowCardView="true"
            ShowToolbar="true"
            ShowSearch="true"
            ShowExtendButtons="true" 
            ShowEmpty="true" 
            SearchModel="@_search" 
            SearchMode="SearchMode.Top"
            EditDialogItemsPerRow="1"
            EditDialogSize="Size.Medium"
            ExtendButtonColumnWidth="195"
            PageItemsSource="@(new int[] { 15, 50 })"
            OnQueryAsync="@OnQueryAsync"
            OnSaveAsync="@OnSaveAsync"
            OnDeleteAsync="@OnDeleteAsync">
        <TableColumns>
            <TableColumn @bind-Field="@context.Name" />
            <TableColumn @bind-Field="@context.Email" />
            <TableColumn @bind-Field="@context.AdminName"/>
            <TableColumn @bind-Field="@context.Phone" />
            <TableColumn @bind-Field="@context.CreatedTime" Width="180" FormatString="yyyy-MM-dd HH:mm:ss"/>
            <TableColumn @bind-Field="@context.Remark" />
        </TableColumns>
        <EditTemplate>
                <div class="row g-3 form-inline">
                    <div class="col-sm-6">
                        <BootstrapInput @bind-Value="@context.Name" maxlength="50" />
                    </div>

                    <div class="col-sm-6">
                        <BootstrapInput @bind-Value="@context.AdminName" maxlength="50" />
                    </div>

                    <div class="col-sm-6">
                        <BootstrapInput @bind-Value="@context.Email" maxlength="50" />
                    </div>

                    <div class="col-sm-6">
                        <BootstrapInput @bind-Value="@context.Phone" maxlength="50" />
                    </div>

                    <div class="col-sm-12">
                        <Textarea @bind-Value="@context.Remark" maxlength="50"></Textarea>
                    </div>
                </div>
        </EditTemplate>
        <RowButtonTemplate>
            <DropdownButton Text="更多" 
                Items="@(new List<DropdownButtonItem>()
                {
                    new("授权菜单",()=> OnOwnMenuAsync(context)),
                    new("重置密码",()=> OnResetPwdAsync(context),"确认重置选中行的密码？")
                })"/>
        </RowButtonTemplate>
        <SearchTemplate>
            <GroupBox Title="搜索条件">
                <div class="row g-3 form-inline">
                    <div class="col-12 col-sm-6">
                        <BootstrapInput @bind-Value="@context.Name" ShowLabel="true"/>
                    </div>
                </div>
            </GroupBox>
        </SearchTemplate>
    </Table>
</div>

@code{
    private TenantOutput _search = new();
    private IEnumerable<SelectedItem>? _requestTypes;
    private IEnumerable<SelectedItem>? _timerTypes;
    private IEnumerable<SelectedItem>? _executeTypes;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        _requestTypes = typeof(RequestTypeEnum).ToSelectList();
        _timerTypes = typeof(SpareTimeTypes).ToSelectList();
        _executeTypes = typeof(SpareTimeExecuteTypes).ToSelectList();
    }

    private Task<QueryData<TenantOutput>> OnQueryAsync(QueryPageOptions options)
        => SysTenantService.PageAsync(_search.Adapt<TenantPageInput>(options));

    private Task<bool> OnDeleteAsync(IEnumerable<TenantOutput> items) 
        => SysTenantService.DeleteAsync(items);

    private async Task<bool> OnSaveAsync(TenantOutput item, ItemChangedType type)
    {
        var result = false;
        switch (type)
        {
            case ItemChangedType.Add:
                result = await SysTenantService.AddAsync(item);
                break;
            case ItemChangedType.Update:
                result = await SysTenantService.EditAsync(item);
                break;
        }
        return result;
    }

    private async Task OnOwnMenuAsync(TenantOutput item)
    {
        var items = SysTenantService.OwnMenu(item);
        await DialogService.Show(new DialogOption()
        {
            Title = "租户授权菜单",
            IsCentered = true,
            ShowFooter = true,
            ShowSaveButton = true,
            OnSaveAsync = (async () =>
            {
                var ids = items?.GetCheckedKeys<long>();
                var result = await SysTenantService.GrantMenuAsync(ids, item);
                await Toast!.Success("租户授权菜单", $"操作{(result ? "成功" : "失败")}。");
                return result;
            }),
            BodyTemplate = @<Scroll Height="480px"><Tree Items=@items ShowCheckbox="true"/></Scroll>
        });
    }

    private async Task OnResetPwdAsync(TenantOutput item)
    {
        var result = await SysTenantService.ResetPwdAsync(item);
        await Toast!.Success("重置密码", $"操作{(result ? "成功" : "失败")}。");
    }
}